package com.yunhe.policyfulfillment.authority.service.impl;

import com.yunhe.common.exception.ArgumentErrorException;
import com.yunhe.common.util.DateTimeUtils;
import com.yunhe.common.util.MD5Util;
import com.yunhe.policyfulfillment.authority.domain.PolicyOrg;
import com.yunhe.policyfulfillment.authority.domain.PolicyRole;
import com.yunhe.policyfulfillment.authority.domain.PolicyUser;
import com.yunhe.policyfulfillment.authority.repository.IPolicyOrgRepository;
import com.yunhe.policyfulfillment.authority.service.IPolicyOrgService;
import com.yunhe.policyfulfillment.authority.service.IPolicyUserService;
import com.yunhe.policyfulfillment.util.MyHeaderUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import com.yunhe.common.util.CollectionUtil;

import java.util.Date;
import java.util.List;

/**
 * @ClassName PolicyOrgServiceImpl
 * @Description TODO
 * @Author HeTao
 * @Date 2021/7/26 9:39
 * @Version 1.0
 **/
@Service
public class PolicyOrgServiceImpl implements IPolicyOrgService {

    @Autowired
    private IPolicyOrgRepository policyOrgRepository;

    @Autowired
    private MyHeaderUtil myHeaderUtil;

    @Autowired
    private IPolicyUserService policyUserService;

    @Override
    public List<PolicyOrg> findPolicyOrgs() {
        return policyOrgRepository.findPolicyOrgs();
    }

    @Override
    public List<PolicyOrg> findPolicyOrgsByOrgType(String orgType) {
        return policyOrgRepository.findPolicyOrgsByOrgType(orgType);
    }

    @Override
    public List<PolicyOrg> findPartyGovernmentOfficeAndCompetentAuthority() {
        return policyOrgRepository.findPartyGovernmentOfficeAndCompetentAuthority();
    }

    @Override
    public PolicyOrg findPolicyOrgByOrgId(int orgId) {
        return policyOrgRepository.searchPolicyOrgByPrimaryKey(orgId);
    }

    @Override
    public PolicyOrg searchPolicyOrgByPrimaryKey(Integer orgId) {
        return policyOrgRepository.searchPolicyOrgByPrimaryKey(orgId);
    }

    @Override
    public void createPolicyOrg(PolicyOrg policyOrg) {
        policyOrg.setCreateTime(new Date());
        policyOrgRepository.createPolicyOrgSelective(policyOrg);
    }

    @Override
    public void deletePolicyOrg(List<Long> ids) throws ArgumentErrorException {
        if(!CollectionUtil.isEmpty(ids)){
            for (long id : ids) {
                List<PolicyUser> policyUsers = policyUserService.findPolicyUsersByOrgId(Math.toIntExact(id));
                if(!CollectionUtil.isEmpty(policyUsers)){
                    throw new ArgumentErrorException("当前部门已关联用户，请先删除用户！");
                }
                policyOrgRepository.deletePolicyOrgByPrimaryKey(new Long(id).intValue());
            }
        }
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void updatePolicyOrg(PolicyOrg policyOrg) throws ArgumentErrorException {
        PolicyUser headerUser = myHeaderUtil.getHeaderUser();
        if (headerUser == null) {
            throw new ArgumentErrorException("登录信息已失效,请重新登录!");
        }
        policyOrg.setUpdateTime(new Date());
        policyOrgRepository.updatePolicyOrgByPrimaryKeySelective(policyOrg);
    }
}
